package com.hilihili.service.impl;

import cn.hutool.http.HttpResponse;
import com.hilihili.config.TranscodeConfig;
import com.hilihili.service.IStaticResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author machi
 * @date 2023/12/19
 */
@Slf4j
@Service
public class StaticResourceServiceImpl implements IStaticResourceService {
    @Autowired
    TranscodeConfig transcodeConfig;

    @Override
    public void getVideo(String url, HttpServletResponse response) {
        //获取视频文件流
        FileInputStream fileInputStream = null;
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            // 通过fileName读取服务器文件
            fileInputStream = new FileInputStream(transcodeConfig.getSource() + url);
            byte[] cache = new byte[1024];
            //判断是m3u8文件还是ts 文件
            if (url.endsWith(".ts")) {
                //ts 文件
                response.setHeader(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
            } else {
                //m3u8文件
                response.setHeader(HttpHeaders.CONTENT_TYPE, "audio/mpegurl");
            }
            //HttpHeaders.CONTENT_LENGTH 返回文件长度给前端
            response.setHeader(HttpHeaders.CONTENT_LENGTH, fileInputStream.available() + "");

            //读取文件
            int flag;
            while ((flag = fileInputStream.read(cache)) != -1) {
                outputStream.write(cache, 0, flag);
            }
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            log.error("文件传输错误", e);
            throw new RuntimeException("文件传输错误");
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    log.error("流释放错误", e);
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    log.error("文件流释放错误", e);
                }
            }
        }
    }

    @Override
    public void getImage(String imageUrl, HttpServletResponse response) {
        //获取视频文件流
        if (imageUrl == null || imageUrl.isEmpty()){
            return;
        }
        FileInputStream fileInputStream = null;
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            // 通过fileName读取服务器文件
            fileInputStream = new FileInputStream(transcodeConfig.getSource() + imageUrl);
            byte[] cache = new byte[1024];
            //读取文件
            int flag;
            while ((flag = fileInputStream.read(cache)) != -1) {
                outputStream.write(cache, 0, flag);
            }
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            log.error("文件传输错误", e);
            throw new RuntimeException("文件传输错误");
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    log.error("流释放错误", e);
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    log.error("文件流释放错误", e);
                }
            }
        }
    }


}
